$(function () {
  // 根据配置决定是否缩放
  if (CONFIG && CONFIG.scale) {
    $('body').css('overflow', 'hidden');
    initScale();
    $(window).resize(function () {
      initScale();
    });
  }
  // 根据是否具有头部时间开启时间
  // if ($('.header-datetime').length) {
  //   // 开启时间
  //   initTime();
  // }
});
// 获取缩放比例
function getScale() {
  var appDom = $('#app');
  var currentWidth = parseFloat(appDom.css('width')),
  currentHeight = parseFloat(appDom.css('height')),
  winWidth = $(window).width(),
  winHeight = $(window).height(),
  widthScale = winWidth / currentWidth,
  heightScale = winHeight / currentHeight;
  return {
    width: widthScale,
    height: heightScale
  };
}
// 执行页面缩放
function initScale() {
  var scale = getScale();
  $('#app').css({
    'transform': 'scale(' + scale.width + ',' + scale.height + ')'
  });
}
function getTime(){
  var currentTime = new Date()
  var year = currentTime.getFullYear() + '';
  var month = currentTime.getMonth() + 1 + '' >= 10 ? currentTime.getMonth() + 1 + '' : '0' + (currentTime.getMonth() + 1);
  var date = currentTime.getDate() >= 10 ? currentTime.getDate() + '' : '0' + currentTime.getDate();
  var hour = currentTime.getHours() >= 10 ? currentTime.getHours() + '' : '0' + currentTime.getHours();
  var minutes = currentTime.getMinutes() >= 10 ? currentTime.getMinutes() + '' : '0' + currentTime.getMinutes();
  var seconds = currentTime.getSeconds() >= 10 ? currentTime.getSeconds() + '' : '0' + currentTime.getSeconds();
  var ymd = year + '-' + month + '-' + date + '';
  var week = currentTime.getDay();
  var weekArr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  var datetime = year + '-' + month + '-' + date + '   ' + hour + ':' + minutes+ ':' + seconds;
  var hms = hour + ':' + minutes+ ':' + seconds;
  $("#datetime-ymd").text(ymd);
  $("#datetime-week").text(weekArr[week]);
  $("#datetime-hms").text(hms);
}
function initTime(){
  setTimeout(getTime);
  setInterval(getTime,1000);
}
// 裁剪字符串长度超出显示省略号
function cutString(str, len) {
  if (str.length * 2 <= len) {
    return str;
  }
  var strlen = 0;
  var s = "";
  for (var i = 0; i < str.length; i++) {
    s = s + str.charAt(i);
    if (str.charCodeAt(i) > 128) {
      strlen = strlen + 2;
      if (strlen >= len) {
        return s.substring(0, s.length - 1) + "...";
      }
    } else {
      strlen = strlen + 1;
      if (strlen >= len) {
        return s.substring(0, s.length - 2) + "...";
      }
    }
  }
  return s;
}

// 创建 echarts 实例
function createChart(dom, theme, option){
  var chart = echarts.init(dom, theme || 'chalk')
  chart.setOption(option || {})
  return chart
}

// 图表tooltip自动更新
function loopChartTip (chart, chartData) {
  var loopIndex = 0;
  var timer = setInterval(showTip, 2000);
  var dom = $(chart.getDom());
  dom.hover(function () {
    clearInterval(timer);
  }, function () {
    timer = setInterval(showTip, 2000);
  });
  showTip();
  function showTip() {
    chart.dispatchAction({
      type: 'showTip',
      seriesIndex: 0,
      dataIndex: loopIndex
    });
    loopIndex++;
    if (loopIndex >= chartData.length) {
      loopIndex = 0;
    }
  }
}
